<?php
class Season extends AppModel
{

	var $name = 'Season';
	var $validate = array
	(
		'season_name' => array(
				'rule' => 'notempty',
				'required' => true,
				'message' => 'Enter the season name'
			),
		'start_date' => array(
				'rule' => 'validate_start_date', 
				'required' => true,
				'message' => 'Please insert a valid date'
			),
		'end_date' => array(
				'rule' => 'validate_end_date', 
				'required' => true,
				'message' => 'End date must be after start date'
			),
		'description' => array(
				'rule' => 'notempty',
				'message' => 'Do not leave the description field empty'
			)
	);

	//The Associations below have been created with all possible keys, those that are not needed can be removed
	var $hasMany = array
	(
		'Event' => array
		(
			'className' => 'Event',
			'foreignKey' => 'season_id',
			'dependent' => true,
			'counterCache' => true
		)
	);
	
	
	function validate_start_date($date) {
		$date = $date['start_date'];
		return $this->validate_date($date);
	}
	
	function validate_end_date($date) {
		$startDate = $this->data['Season']['start_date'];
		$endDate = $date['end_date'];
		if ($this->validate_date($endDate)) {
			$start = strtotime($startDate);
			$end = strtotime($endDate);
			if ($end > $start) {
				return true;
			}
		}
		return false;
	}
	

}
?>